/**
 * 商品详情页
 * Created by shenc on 2016/9/18.
 */

//商品详情页
function goodsFun(config) {
    var goodsNum=$('#goodsNum'),
        coverWrap=$('#goodsCover'),
        cover=coverWrap.find('.ui-cover'),
        mask=coverWrap.find('.ui-cover-mask'),
        buyBtn=$('#buy'),
        addCartBtn=$('#addCartBtn'),
        goodId=coverWrap.attr('data-id'),
        cartNum=$('.J-carNum');
    //弹框中的选项
    var coverBtn=cover.find('.J-btn'),
        coverNum=cover.find('.ui-num-input'),
        coverTags=cover.find('.ui-tag');
    var scroll,flag=0;

    goodsNum.click(function () {showCover({type:'num'});});
    buyBtn.click(function () {showCover({type:'buy'});});
    addCartBtn.click(function () {showCover({type:'addCart'});});

    cover.on('click','.ui-icon-return',function () {hideCover();});
    coverNum.on('change',function () {goodsNum.find('i').text($(this).val());});

    coverTags.on('click',function () {
        var that=$(this);
        if(that.hasClass('current')) return false;
        that.closest('ul').find('.current').removeClass('current');
        that.addClass('current');

        var ajaxData={attributValueArr:[]};
        var lk=Locked();
        that.closest('.ui-tags-list').find('.current').each(function () {ajaxData.attributValueArr.push($(this).attr('data-id'));});
        $.ajax({url:config.changeAttribute+'/'+goodId,type:'post',dataType:'json',data:ajaxData,
            success:function (res) {
                renderCover(res);
            },
            error:function (e) {
                topTip(e.message||Global.message['404'],{type:'warn'});
            },
            complete:function () {
                UnLocked(lk);
            }
        });

    });

    //修改邮费
    $('#areaInput').on('input change',function () {
        var numItem = $('.js-expressPrice'), input = $('#areaIds'), id = input.val();
        var ids = id.split(',');
        //获取运费信息
        if (ids.length) {
            if (ids[0] == 650000 || ids[0] == 540000) {
                numItem.text(15);
            } else {
                numItem.text(7);
            }
        }
    });

    function showCover(data) {
        var goodsStock=coverNum.attr('max');
        switch (data.type){
            case 'num':
                coverBtn.removeClass('disabled').prop('disabled',false);
                coverBtn.text('确定');
                coverBtn.off('click').on('click',function () {hideCover();});
                break;
            case 'buy':
                coverBtn.text('立即购买');
                if(goodsStock==0){
                    coverBtn.addClass('disabled').prop('disabled',true);
                    coverBtn.off('click');
                    break;
                }
                coverBtn.off('click').on('click',function () {
                    if(typeof config=='undefined') {
                        topTip('页面参数无效',{type:'warn'});
                        return false;
                    }
                    var formData={
                        cartGoods:[{
                            goods_id:cover.attr('data-id'),
                            buy_num:coverNum.val()
                        }]
                    };
                    var lk=Locked();
                    $.ajax({url:config.buy,type:'post',dataType:'json',data:formData,
                        success:function (res) {
                            if(res.status){
                                window.location.href=config.order;
                            }else{
                                if(res.message=='请登录'){
                                    window.location.href='/user/login?return_url='+window.location.href;
                                }else{
                                    topTip(res.message,{type:'warn'});
                                }
                            }
                        },
                        error:function (e) {
                            topTip(e.message||Global.message['404'],{type:'warn'});
                        },
                        complete:function () {
                            UnLocked(lk);
                        }
                    });

                });
                break;
            case 'addCart':
                coverBtn.text('加入购物车');
                if(goodsStock==0){
                    coverBtn.addClass('disabled').prop('disabled',true);
                    coverBtn.off('click');
                    break;
                }
                coverBtn.off('click').on('click',function () {
                    var ajaxData={
                        goods_id:cover.attr('data-id'),
                        goods_num:coverNum.val(),
                        _token:_token||''
                    };
                    var lk=Locked();
                    $.ajax({url:config.addCart,type:'post',dataType:'json',data:ajaxData,
                        success:function (res) {
                            if(res.goods_num){
                                hideCover(function(){
                                    topTip(res.message||'已加入购物车');
                                    cartNum.text(res.goods_num).show();
                                });
                            }else{
                                topTip(res.message,{type:'warn'});
                            }
                        },
                        error:function (e) {
                            topTip(e.message||Global.message['404'],{type:'warn'});
                        },
                        complete:function () {
                            UnLocked(lk);
                        }
                    });
                });
                break;
            default:
                break;
        }

        flag=1;
        coverWrap.show();
        scroll = new fz.Scroll('.ui-cover-wrap', {
            scrollY: false,
            scrollX: true
        });
        scroll.scrollToElement('.ui-cover',500,true,true);
        scroll.disable();
        setTimeout(function () {
            mask.css('visibility','hidden');
        },800);
    }
    function hideCover(cb) {
        flag=0;
        scroll.enable();
        setTimeout(function () {
            scroll.scrollToElement('.ui-cover-mask',500,true,true);
        },10);
        setTimeout(function () {
            coverWrap.hide();
            mask.css('visibility','visible');
            scroll.destroy();
        },800);
        typeof cb === 'function' && cb();
    }
    function renderCover(data) {
        cover.attr('data-id',data.goods_info.goods_id);
        cover.find('.J-thumb').replaceWith('<span class="J-thumb" style="background-image: url('+data.goods_info.thumb+');"></span>');
        cover.find('.J-name').text(data.goods_info.goods_name);
        cover.find('.J-price').text(data.goods_info.goods_real_price);
        cover.find('.J-stock').text(data.goods_info.goods_number>10?'(库存充足)':data.goods_info.goods_number>0?'(库存'+data.goods_info.goods_number+'件)':'(无库存)');
        coverNum.attr('max',data.goods_info.goods_number);
        cover.find('.ui-num-plugin').toggle(data.goods_info.goods_number!==0)
    }

    document.addEventListener('touchmove', function (event) {
        if(flag==1){
            event.preventDefault();
        }
    });
}
